using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using XNAProjectLibrary;
using System.Windows.Forms;


namespace XNAProject.GameElements
{
    public class FleetLoader : Microsoft.Xna.Framework.DrawableGameComponent
    {
        float scale;

        SpriteFont font;

        List<string> Text;
        int selected;

        bool action;

        Texture2D background;
        Vector2 backgroundPosition;

        SaveFleet fleet;

        XNAGame OurGame;
        private IInputHandler Input;
        PrepareStatePanel panel;

        public FleetLoader(Game game, ref SaveFleet Fleet, PrepareStatePanel Panel)
            : base(game)
        {
            GraphicsDeviceManager graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));
            Input = (IInputHandler)Game.Services.GetService(typeof(IInputHandler));
            OurGame = (XNAGame)game;

            panel = Panel;
            fleet = Fleet;

            action = false;
            Text = new List<string>();
            selected = 0;
            scale = 0.61f * graphics.PreferredBackBufferWidth / 1000;
        }

        protected override void LoadContent()
        {
            font = Game.Content.Load<SpriteFont>(@"Fonts\description");

            background = Game.Content.Load<Texture2D>("Textures/FleetLoader/background");
            backgroundPosition = new Vector2(Game.GraphicsDevice.Viewport.Width / 2 - background.Width * scale / 2,
                Game.GraphicsDevice.Viewport.Height / 2 - background.Height * scale / 2);

            base.LoadContent();
        }

        public void Start()
        {
            OurGame.Components.Add(this);

            Text.Clear();

            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + @"\Fleets");
            if (di.Exists)
            {
                FileInfo[] subFiles = di.GetFiles("*.m8", SearchOption.AllDirectories);

                foreach (FileInfo file in subFiles)
                {
                    Text.Add(file.Name);
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.DPadUp, Microsoft.Xna.Framework.Input.Keys.Up))
            {
                if (selected != 0)
                    selected--;
            }
            if (Input.WasPressed(0, Buttons.DPadDown, Microsoft.Xna.Framework.Input.Keys.Down))
            {
                if (selected != Text.Count - 1)
                    selected++;
            }
            if (Input.WasPressed(0, Buttons.A, Microsoft.Xna.Framework.Input.Keys.Space))
            {
                fleet.Load(Text[selected]);
                panel.LoadFleet();
                OurGame.Components.Remove(this);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.Draw(background, backgroundPosition, null, Color.White, 0f, Vector2.Zero,
                scale * 2f, SpriteEffects.None, 0.1f);

            for (int i = 0; i < Text.Count; i++)
            {
                if (i == selected)
                {
                    OurGame.spriteBatch.DrawString(font, Text[i] + "\n", new Vector2(backgroundPosition.X + 120 * scale, backgroundPosition.Y + 40 + 20 * i * scale),
                            Color.Red, 0f, Vector2.Zero, scale * 1.5f, SpriteEffects.None, 0.2f);
                }
                else
                {
                    OurGame.spriteBatch.DrawString(font, Text[i] + "\n", new Vector2(backgroundPosition.X + 120 * scale, backgroundPosition.Y + 40 + 20 * i * scale),
                            Color.White, 0f, Vector2.Zero, scale * 1.5f, SpriteEffects.None, 0.2f);
                }
            }

            OurGame.spriteBatch.DrawString(font, "Up or Down Arrows \n Space for loading", new Vector2(50, 50),
                            Color.White, 0f, Vector2.Zero, scale * 2f, SpriteEffects.None, 0.2f);

            OurGame.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}